package com.sdy.dataexchange.meta.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.dataadapter.DataAdapter;
import com.sdy.dataexchange.biz.model.ExDbDict;
import com.sdy.dataexchange.meta.mapper.MetadataModelMapper;
import com.sdy.dataexchange.meta.model.MetadataModel;
import com.sdy.dataexchange.meta.service.MetadataModelService;
import com.sdy.dataexchange.meta.util.ConstantUtil;
import com.sdy.dataexchange.meta.util.JdbcUtil;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wyy
 * @since 2019-10-25
 */
@Slf4j
@Service
public class MetadataModelServiceImpl extends BaseServiceImpl<MetadataModel> implements MetadataModelService {
    @Autowired
    private JdbcUtil jdbcUtil;
    @Autowired
    private MetadataModelMapper metadataModelMapper;


    /**
     * 查询所有Table模型数量
     *
     * @param dbId
     * @param exDbDict
     * @return
     */
    @Override
    public Integer queryDbMetadataModel(Integer dbId, ExDbDict exDbDict) {
        DataAdapter dataAdapter = jdbcUtil.getDataAdapter(exDbDict);
        List<Map<String, Object>> maps = null;
        if (exDbDict.getDbType().equals("MYSQL")) {
            maps = dataAdapter.executeQuery("select COUNT(*) from information_schema.tables where table_schema='dataexchange'");
        } else if (exDbDict.getDbType().equals("ORACLE")) {
            maps = dataAdapter.executeQuery("SELECT COUNT(*) AS tableSize from user_tables");
        }
        Integer tablesize = Integer.parseInt(maps.get(0).get("TABLESIZE").toString());
        return tablesize;
    }

    /**
     * 查询所有Table模型
     *
     * @param dbId
     * @param exDbDict
     * @param page
     * @return
     */
    @Override
    public List<MetadataModel> queryDbMetadataModel(Integer dbId, ExDbDict exDbDict, Page<MetadataModel> page) {
        DataAdapter dataAdapter = jdbcUtil.getDataAdapter(exDbDict);
        List<MetadataModel> list = new ArrayList<>();
        List<Map<String, Object>> maps = null;
        if (exDbDict.getDbType().equals("MYSQL")) {
            maps = dataAdapter.executeQuery(String.format(ConstantUtil.MYSQL_ALL_TABLE,
                    (page.getCurrent() - 1) * page.getSize(), page.getSize()));
        } else if (exDbDict.getDbType().equals("ORACLE")) {
            maps = dataAdapter.executeQuery(String.format(ConstantUtil.ORACLE_ALL_TABLE,
                    page.getSize() * page.getCurrent(), (page.getCurrent() - 1) * page.getSize()));
        } else {
            log.error("数据源类型有误,暂不支持此类型数据源!");
        }
        Date d = new Date();
        for (int a = 0; a < maps.size(); a++) {
            MetadataModel metadataModel = new MetadataModel();
            metadataModel.setModelName((String) maps.get(a).get("TABLE_NAME"));
            metadataModel.setModelType(1);
            metadataModel.setModelNewdata(d);
            list.add(metadataModel);

        }
        return list;
    }

    @Override
    public List<MetadataModel> queryDbTabMod(Integer dbId) {
        return metadataModelMapper.queryDbTabMod(dbId);
    }

    @Override
    public List<MetadataModel> queryAllTabMod() {
        return metadataModelMapper.queryAllTabMod();
    }

    /**
     * JDBC 查询 源数据库底下的 Tab Mod
     *
     * @param exDbDictc
     * @return
     */
    @Override
    public List<MetadataModel> queryAllTabMod(ExDbDict exDbDictc) {
        Integer dbId = exDbDictc.getDbId();
        String dbType = exDbDictc.getDbType();
        String dbDesc = exDbDictc.getDbDesc();
        String dbIp = exDbDictc.getDbIp();
        String dbUser = exDbDictc.getDbUser();
        String dbName = exDbDictc.getDbName();
        DataAdapter dataAdapter = jdbcUtil.getDataAdapter(exDbDictc);
        String sql = "SELECT TABLE_NAME FROM user_tables";
        List<Map<String, Object>> maps = dataAdapter.executeQuery(sql);
        List<MetadataModel> models = new ArrayList<>();
        for (Map m : maps) {
            MetadataModel model = new MetadataModel();
            model.setDbId(dbId);
            model.setModelName((String) m.get("TABLE_NAME"));
            model.setModelType(1);
            model.setModelIdentifying(dbIp + "/" + dbName + "/" + dbUser);
            model.setModelCoding(dbType);
            models.add(model);
        }
        return models;
    }

    /**
     * 把查出来的Tab Mod存储到本地
     *
     * @param models
     * @return
     */
    @Override
    public Integer addAllTabMod(List<MetadataModel> models) {
        Integer integer = metadataModelMapper.addAllTabMod(models);
        return integer;
    }

    @Override
    public Integer delByIdTabMod(Integer dbId, String tabName) {
        Integer integer = metadataModelMapper.delByIdTabMod(dbId, tabName);
        return integer;
    }

    @Override
    public List<MetadataModel> queryByIdTabMod(Integer dbId, String tabName) {
        return metadataModelMapper.queryByIdTabMod(dbId, tabName);
    }

    @Override
    public Integer addByTabMod(MetadataModel metadataModel) {
        return metadataModelMapper.addByTabMod(metadataModel);
    }

    /**
     * 查询本地库中已经存入 模型
     *
     * @param dbId
     * @return
     */
    @Override
    public List<MetadataModel> queryByDbIdMod(Integer dbId) {
        return metadataModelMapper.queryByDbIdMod(dbId);
    }
}
